package com.niiwoo.civet.trade.listener.antifraud;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.niiwoo.civet.trade.constant.LoanFlowMqConstant;
import com.niiwoo.civet.trade.dao.entity.ProjectFlowExecuteRuntime;
import com.niiwoo.civet.trade.dao.mapper.ProjectFlowExecuteRuntimeMapperExt;
import com.niiwoo.civet.trade.enums.ProjectTypeEnum;
import com.niiwoo.civet.trade.service.loan.antifraud.LoanAntiFraudDubboService;
import com.niiwoo.civet.trade.service.local.flowexecute.ProjectFlowExecuteService;
import com.niiwoo.civet.trade.service.local.loan.antifraud.LoanAntiFraudLocalService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.charset.Charset;

/**
 * Created by lujing on 2018/4/19.
 */
@Slf4j
@Service
public class LoanAntiFraudListener {
    @Reference(version = "1.0.0")
    private LoanAntiFraudDubboService loanAntiFraudDubboService;
    @Autowired
    private ProjectFlowExecuteService projectFlowExecuteService;
    @Autowired
    private LoanAntiFraudLocalService loanAntiFraudLocalService;
    @Autowired
    private ProjectFlowExecuteRuntimeMapperExt projectFlowExecuteRuntimeMapperExt;

    @RabbitListener(queues = {LoanFlowMqConstant.Queue.LOAN_ANTI_FRAUD_QUEUE})
    public void loanProcess(Message message, Channel channel)throws IOException{
        try {
            log.info("5.1.5借款反欺诈Listener{}",message.getBody());
            String reqMsg = new String(message.getBody(), Charset.forName("UTF-8"));
            Long flowExecuteId = JSON.parseObject(reqMsg).getLong("flowExecuteId");
            ProjectFlowExecuteRuntime projectFlowExecuteRuntime = projectFlowExecuteRuntimeMapperExt.selectByPrimaryKey(flowExecuteId);
            ProjectTypeEnum projectTypeEnum = null;
            String userId = projectFlowExecuteRuntime.getBorrowUserId();
            Long projectId = projectFlowExecuteRuntime.getProjectId();
            // 是否为复借用户
            Boolean isMultiBorrow = loanAntiFraudLocalService.firstLoan(userId);
            if(projectFlowExecuteRuntime != null){
                projectTypeEnum = ProjectTypeEnum.getEnum(projectFlowExecuteRuntime.getProjectType());
            }

            if(projectTypeEnum == null){
                return;
            }
            switch (projectTypeEnum){
                case SMALL_LOAN:
                    if(isMultiBorrow){
                        loanAntiFraudDubboService.libraFraudSmallLoanNotFirst(userId,projectId,flowExecuteId);
                    }else{
                        loanAntiFraudDubboService.libraFraudSmallLoanFirst(userId,projectId,flowExecuteId);
                    }
                    break;
                case SOCIAL_SECURITY_LOAN:
                    if(isMultiBorrow){
                        loanAntiFraudDubboService.libraFraudSocialSecurityNotFirst(userId,projectId,flowExecuteId);
                    }else {
                        loanAntiFraudDubboService.libraFraudSocialSecurityFirst(userId,projectId,flowExecuteId);
                    }
                    break;
                case CREDIT_CARD_LOAN:
                    if(isMultiBorrow){
                        loanAntiFraudDubboService.libraFraudCreditCardNotFirst(userId,projectId,flowExecuteId);
                    }else{
                        loanAntiFraudDubboService.libraFraudCreditCardFirst(userId,projectId,flowExecuteId);
                    }
                    break;
                case CONSUME_LOAN:
                	if(isMultiBorrow){
                		loanAntiFraudDubboService.libraFraudConsumeLoanAgain(userId, projectId, flowExecuteId);
                	} else {
                		loanAntiFraudDubboService.libraFraudConsumeLoan(userId, projectId, flowExecuteId);
                	}
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            log.error("卡牛授信反欺诈异常,flowExecuteIdMsg={}, message={}, e={}", new String(message.getBody(), Charset.forName("UTF-8")), JSON.toJSONString(message), e);
        } finally {
            doAckAnswer(message, channel);
        }
    }

    private void doAckAnswer(Message message, Channel channel) {
        try {
            //确认消息已经接收;
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException ie) {
            log.error("credit model ack occur exception, the reason is : {}", ie);
        }
    }
}
